package cn.iocoder.yudao.module.system.controller.admin.mini.group.goods.service.groupgoods;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goods.dal.dataobject.goods.GoodsDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goods.dal.mysql.goods.GoodsMapper;
import cn.iocoder.yudao.module.system.controller.admin.mini.group.goods.controller.admin.groupgoods.vo.GroupGoodsExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.group.goods.controller.admin.groupgoods.vo.GroupGoodsPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.group.goods.dal.dataobject.groupgoods.GroupGoodsDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.group.goods.dal.mysql.groupgoods.GroupGoodsMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 拼团商品 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class GroupGoodsServiceImpl implements GroupGoodsService {

    @Resource
    private GroupGoodsMapper groupGoodsMapper;
    @Resource
    GoodsMapper goodsMapper;

    @Override
    public Integer createGroupGoods(GroupGoodsDO dto) {
        if (dto.getGoodsId()==null)
            throw new ServiceException(500,"goodsId不能为空");
        GoodsDO goodsDO = goodsMapper.selectById(dto.getGoodsId());
        if (goodsDO==null)
            throw new ServiceException(500,"商品为空，系统异常");

        dto.setGoodsName(goodsDO.getName());
        dto.setActNum(dto.getSetUpNum());
        dto.setSoldNum(0);
        dto.setImg(goodsDO.getImg());
        dto.setContent(dto.getContent());
        // 插入
        groupGoodsMapper.insert(dto);
        // 返回
        return dto.getId();
    }

    @Override
    public void updateGroupGoods(GroupGoodsDO dto) {
        // 校验存在
        validateGroupGoodsExists(dto.getId());
        // 更新
        groupGoodsMapper.updateById(dto);
    }

    @Override
    public void deleteGroupGoods(Integer id) {
        // 校验存在
        validateGroupGoodsExists(id);
        // 删除
        groupGoodsMapper.deleteById(id);
    }

    private void validateGroupGoodsExists(Integer id) {
        if (groupGoodsMapper.selectById(id) == null) {
            throw exception(500);
        }
    }

    @Override
    public GroupGoodsDO getGroupGoods(Integer id) {
        return groupGoodsMapper.selectById(id);
    }

    @Override
    public List<GroupGoodsDO> getGroupGoodsList(Collection<Integer> ids) {
        return groupGoodsMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<GroupGoodsDO> getGroupGoodsPage(GroupGoodsPageReqVO pageReqVO) {
        PageResult<GroupGoodsDO> page = groupGoodsMapper.selectPage(pageReqVO);
//        List<GroupGoodsDO> list = page.getList();
//        for (GroupGoodsDO vo : list) {
//            List<String> img = vo.getImg();
//            if (img!=null&&img.size()>0)
//                vo.setImgUrl(img.get(0));
//        }
//        page.setList(list);
        return page;
    }

    @Override
    public List<GroupGoodsDO> getGroupGoodsList(GroupGoodsExportReqVO exportReqVO) {
        return groupGoodsMapper.selectList(exportReqVO);
    }

}
